/*
 *  Copyright 2008-2010 www.jrails.org
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.rails.extjs.panel.grid;

import java.util.ArrayList;
import java.util.List;

import org.rails.extjs.Option;
import org.rails.extjs.data.Store;
import org.rails.extjs.javascript.Function;

public class PivotGrid extends GridPanel {
	
	public final static String AGGREGATOR_SUM = "sum";
	public final static String AGGREGATOR_COUNT = "count";
	public final static String AGGREGATOR_AVG = "avg";
	public final static String AGGREGATOR_MIN = "min";
	public final static String AGGREGATOR_MAX = "max";
	
	private final List<PivotAxis> leftAxis = new ArrayList<PivotAxis>();
	private final List<PivotAxis> topAxis = new ArrayList<PivotAxis>();
	
	public PivotGrid() {
		super();
	}

	public PivotGrid(Store store, ColumnModel colModel) {
		super(store, colModel);
	}

	public PivotGrid(Option configOptions) {
		super(configOptions);
	}

	public PivotGrid(String title, Store store, ColumnModel colModel) {
		super(title, store, colModel);
	}

	public PivotGrid(String id, String title, Store store, ColumnModel colModel) {
		super(id, title, store, colModel);
	}
	
	public void setAggregator(String aggregator){
		addOption("aggregator",aggregator);
	}
	
	public String getAggregator(){
		return getString("aggregator","");
	}
	
	public void addTopAxis(PivotAxis item){
		topAxis.add(item);
	}
	
	public void insertTopAxis(int index,PivotAxis item){
		topAxis.add(index,item);
	}
	
	public void removeTopAxis(int index){
		topAxis.remove(index);
	}
	
	public void clearTopAxis(){
		topAxis.clear();
	}
	
	public void setMeasure(String measure){
		addOption("measure",measure);
	}
	
	public String getMeasure(){
		return getString("measure","");
	}
	
	public void setRenderer(Function renderer){
		addOption("renderer",renderer);
	}
	
	public Function getRenderer(){
		return (Function)getOption("renderer");
	}
	
	public void addLeftAxis(PivotAxis item){
		leftAxis.add(item);
	}
	
	public void insertLeftAxis(int index,PivotAxis item){
		leftAxis.add(index,item);
	}
	
	public void removeLeftAxis(int index){
		leftAxis.remove(index);
	}
	
	public void clearLeftAxis(){
		leftAxis.clear();
	}
	
	@Override
	public void createItems(){
		super.createItems();
		if(leftAxis.size() > 0)
			addOption("leftAxis",leftAxis);
		if(topAxis.size() > 0)
			addOption("topAxis",topAxis);
	}
	
	@Override
	public String getScript() {
		script = new StringBuffer("");
		createItems();
		script.append("new Ext.grid.PivotGrid(");
		script.append(createOptions());
		script.append(")");
		return script.toString();
	}

}
